package cn.pzhu.pserson.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import cn.pzhu.pserson.model.Logo;
import cn.pzhu.pserson.util.ResponseUtil;
import cn.pzhu.pserson.service.ILogoService;
import cn.pzhu.pserson.dto.LogoDTO;
import cn.pzhu.pserson.dto.LimitShowDTO;
import org.springframework.web.servlet.ModelAndView;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Slf4j

@Controller
@RequestMapping("/logo")
public class LogoController {
    private  final  static Logger logger = LoggerFactory.getLogger(LogoController.class);

    @Autowired
    private ILogoService logoService;


    @RequestMapping(value = "/page/list", method = RequestMethod.GET)
    public ModelAndView index(Model model, String content, @Validated
            LimitShowDTO limitShowDTO, HttpSession session) {

        Map<String, Object> result = new HashMap<String, Object>();

//        List<Map<String, Object>> logoList = logoService.loadMaps(null, null, null, null, limitShowDTO.getPageNum(), limitShowDTO.getPageSize());
//        long count = logoService.count();

        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageNum(limitShowDTO.getPageNum());
        pageInfo.setPageSize(limitShowDTO.getPageSize());
        pageInfo.setList(logoService.loadMaps(null, null, null, null, limitShowDTO.getPageNum(), limitShowDTO.getPageSize()));

        model.addAttribute("list", pageInfo.getList());
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("userid",session.getAttribute("userid"));
        return new ModelAndView("logo/list", (Map<String, ?>) model) ;
    }
    @CrossOrigin
    @RequestMapping(value = "/getLogo", method = RequestMethod.POST, produces = {"text/plain;charset=utf-8","text/html;charset=utf-8"}             )
    @ResponseBody
    public ResponseEntity<String> list(
            @Validated
        LimitShowDTO limitShowDTO, BindingResult errors, HttpServletResponse response) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();

        Map<String, Object> result = new HashMap<String, Object>();

        List<Map<String, Object>> logoList = logoService.loadMaps(null, null, null, null, limitShowDTO.getPageNum(), limitShowDTO.getPageSize());
        long count = logoService.count();

        result.put("logo", logoList.get(0).get("logoname"));

        return ResponseUtil.getResEntityForGetAndJson(result);
    }

    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseEntity<String> show() {
        Map<String, Object> logoModel = logoService.findMap();

        return ResponseUtil.getResEntityForGetAndJson(logoModel);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ResponseEntity<String> create(
        @Validated
        LogoDTO logoDTO, BindingResult errors) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();

        Logo logoModel = logoDTO.toModel();

        return ResponseUtil.getResEntityForPPP(logoService.save(logoModel));
    }

    @RequestMapping(value = "/admin/update", method = RequestMethod.GET)
    public ModelAndView updatelogo(Model model, Integer id,@Validated
            LimitShowDTO limitShowDTO, HttpSession session) {

        Map<String, Object> result = new HashMap<String, Object>();

        List<Map<String, Object>> logoList = logoService.loadMaps(null, null, null, null,1, 1);

        model.addAttribute("job", logoList.get(0));
        model.addAttribute("userid",session.getAttribute("userid"));


        return new ModelAndView("logo/add", (Map<String, ?>) model);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ModelAndView update( Model model,
        @Validated
        LogoDTO logoDTO, BindingResult errors, HttpSession session) {

        Logo logoModel = logoDTO.toModel();
        Boolean state = logoService.update(logoModel);

        logger.info("保存lOGO： " + logoModel.getLogoname() + "结果：" + state );

        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageNum(1);
        pageInfo.setPageSize(6);
        pageInfo.setList(logoService.loadMaps(null, null, null, null, 1, 6));

        model.addAttribute("list", pageInfo.getList());
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("userid",session.getAttribute("userid"));
        return new ModelAndView("logo/list", (Map<String, ?>) model) ;
    }

    @RequestMapping(value = "", method = RequestMethod.DELETE)
    public ResponseEntity<String> destory() {
        return ResponseUtil.getResEntityForDel(logoService.delete());
    }

}
